package com.shz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shz.pojo.Follow;
import com.shz.pojo.User;
import com.shz.pojo.UserLike;
import com.shz.service.FollowService;
import com.shz.service.UserLikeService;
import com.shz.service.UserService;
import com.shz.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shz
 * @since 2024-09-10
 */
@Api(tags = "笔记点赞模块")

@RestController
@RequestMapping("/user-like")
public class UserLikeController {
    @Resource
    private UserLikeService userLikeService;

    /**
     *
     * 关注是true，已关注是false
     */
    @ApiOperation(value = "点赞该笔记")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "所点赞笔记的id", required = true),
            @ApiImplicitParam(name = "isLike", value = "（发送请求后的状态）显示没有点赞为true,显示点赞了为false", required = true),
    })
    @PutMapping("/{id}/{isLike}")
    public Boolean follow(@PathVariable("id") Integer bid, @PathVariable("isLike") Boolean isLike) {
        Integer uid = UserUtil.getCurrentUser().getId();
        if(!isLike){
            UserLike u=new UserLike();
            u.setUid(uid);
            u.setBid(bid);
          return  userLikeService.save(u);
        }else{
            return userLikeService.remove(new QueryWrapper<UserLike>().eq("uid", uid).eq("bid", bid));
        }
    }


    @ApiOperation(value = "我是否点赞该笔记（状态）")
    @ApiImplicitParam(name = "id", value = "笔记的id", required = true)
    @GetMapping("/isLike/{id}")
    public boolean isLike(@PathVariable("id") Integer bid) {
        Integer uid = UserUtil.getCurrentUser().getId();
        UserLike one = userLikeService.query().eq("uid", uid).eq("bid", bid).one();
        return one != null;
    }




}

